<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
    <TITLE>Linux Terminal Simulator - HTML3</TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
    <STYLE TYPE="text/css">
        BODY {
            background-color: #000000;
            color: #00ff00;
            font-family: "Courier New", Courier, monospace;
            font-size: 12px;
            margin: 10px;
        }

        .terminal {
            width: 95%;
            height: 400px;
            background-color: #000000;
            border: 2px solid #008000;
            padding: 10px;
            overflow: auto;
        }

        .header {
            background-color: #008000;
            color: #000000;
            padding: 5px;
            text-align: center;
            font-weight: bold;
            margin-bottom: 10px;
        }

        .output {
            white-space: pre;
            font-family: "Courier New", Courier, monospace;
            font-size: 12px;
        }

        .prompt {
            color: #00ff00;
        }

        .input {
            background-color: #000000;
            color: #00ff00;
            border: 1px solid #008000;
            font-family: "Courier New", Courier, monospace;
            font-size: 12px;
            width: 70%;
        }

        .error {
            color: #ff0000;
        }

        .success {
            color: #00ff00;
        }

        .directory {
            color: #0080ff;
            font-weight: bold;
        }

        .executable {
            color: #00ff00;
            font-weight: bold;
        }

        TABLE {
            border-collapse: collapse;
            color: #00ff00;
        }

        TD, TH {
            border: 1px solid #008000;
            padding: 3px;
        }

        TH {
            background-color: #004000;
        }
    </STYLE>
</HEAD>
<BODY>
    <DIV CLASS="terminal">
        <DIV CLASS="header">Linux Terminal Simulator - HTML3</DIV>
        <DIV CLASS="output" ID="output">Welcome to Linux Terminal Simulator (HTML3)
Type 'help' for available commands

</DIV>
        <P>
            <SPAN CLASS="prompt" ID="prompt">user@linux:~$</SPAN>
            <INPUT TYPE="text" CLASS="input" ID="input" AUTOCOMPLETE="off">
        </P>
    </DIV>

    <SCRIPT LANGUAGE="JavaScript">
        <!--
        function LinuxSim() {
            this.currentPath = '/home/user';
            this.fileSystem = new Object();
            this.fileSystem['/'] = {
                type: 'directory',
                children: new Object()
            };
            this.fileSystem['/'].children['home'] = {
                type: 'directory',
                children: new Object()
            };
            this.fileSystem['/'].children['home'].children['user'] = {
                type: 'directory',
                children: new Object()
            };
            this.fileSystem['/'].children['home'].children['user'].children['test.txt'] = {
                type: 'file',
                content: 'Hello World!\nThis is a test file.'
            };
            this.fileSystem['/'].children['etc'] = {
                type: 'directory',
                children: new Object()
            };
            this.fileSystem['/'].children['etc'].children['passwd'] = {
                type: 'file',
                content: 'root:x:0:0:root:/root:/bin/bash\nuser:x:1000:1000:user:/home/user:/bin/bash'
            };
            
            this.processes = new Array();
            this.processes[0] = { pid: 1, user: 'root', cpu: 0.1, mem: 0.2, command: 'init' };
            this.processes[1] = { pid: 123, user: 'user', cpu: 2.3, mem: 1.5, command: 'bash' };
            
            this.setupEvents();
        }

        LinuxSim.prototype.setupEvents = function() {
            var self = this;
            var input = document.getElementById('input');
            
            if (input.addEventListener) {
                input.addEventListener('keydown', function(e) {
                    if (e.keyCode == 13) {
                        self.executeCommand(input.value);
                        input.value = '';
                    }
                }, false);
            } else if (input.attachEvent) {
                input.attachEvent('onkeydown', function() {
                    if (window.event.keyCode == 13) {
                        self.executeCommand(input.value);
                        input.value = '';
                    }
                });
            }
            
            input.focus();
        }

        LinuxSim.prototype.executeCommand = function(command) {
            this.appendOutput('<SPAN CLASS="prompt">' + document.getElementById('prompt').innerHTML + '</SPAN> ' + command + '\n');

            var args = command.split(' ');
            var cmd = args[0];

            if (cmd == 'help') {
                this.showHelp();
            } else if (cmd == 'ls') {
                this.listFiles();
            } else if (cmd == 'pwd') {
                this.printWorkingDirectory();
            } else if (cmd == 'cd') {
                this.changeDirectory(args[1]);
            } else if (cmd == 'mkdir') {
                this.makeDirectory(args[1]);
            } else if (cmd == 'cat') {
                this.catFile(args[1]);
            } else if (cmd == 'ps') {
                this.showProcesses();
            } else if (cmd == 'clear') {
                this.clearScreen();
            } else if (cmd == 'whoami') {
                this.appendOutput('user\n');
            } else if (cmd == 'date') {
                this.appendOutput(new Date().toString() + '\n');
            } else if (cmd == 'uname') {
                this.appendOutput('Linux simulator 2.4.20 i386 GNU/Linux\n');
            } else if (command != '') {
                this.appendOutput('<SPAN CLASS="error">bash: ' + cmd + ': command not found</SPAN>\n');
            }

            this.updatePrompt();
        }

        LinuxSim.prototype.showHelp = function() {
            var helpText = 'Available commands:\n' +
                '  ls              - List directory contents\n' +
                '  pwd             - Print working directory\n' +
                '  cd [dir]        - Change directory\n' +
                '  mkdir <dir>     - Create directory\n' +
                '  cat <file>      - Display file content\n' +
                '  ps              - Show running processes\n' +
                '  whoami          - Show current user\n' +
                '  date            - Show current date/time\n' +
                '  uname           - Show system information\n' +
                '  clear           - Clear screen\n' +
                '  help            - Show this help\n\n';
            this.appendOutput(helpText);
        }

        LinuxSim.prototype.listFiles = function() {
            var currentDir = this.getCurrentDirectory();
            if (currentDir && currentDir.type == 'directory') {
                for (var name in currentDir.children) {
                    var item = currentDir.children[name];
                    var className = item.type == 'directory' ? 'directory' : '';
                    this.appendOutput('<SPAN CLASS="' + className + '">' + name + '</SPAN>  ');
                }
                this.appendOutput('\n');
            }
        }

        LinuxSim.prototype.printWorkingDirectory = function() {
            this.appendOutput(this.currentPath + '\n');
        }

        LinuxSim.prototype.changeDirectory = function(path) {
            if (!path || path == '~') {
                this.currentPath = '/home/user';
                return;
            }

            if (path == '..') {
                var parts = this.currentPath.split('/');
                var newParts = new Array();
                for (var i = 0; i < parts.length; i++) {
                    if (parts[i] != '') newParts.push(parts[i]);
                }
                if (newParts.length > 0) {
                    newParts.pop();
                    this.currentPath = '/' + newParts.join('/');
                    if (this.currentPath == '/') this.currentPath = '/';
                }
                return;
            }

            var newPath = this.currentPath == '/' ? '/' + path : this.currentPath + '/' + path;
            var targetDir = this.getDirectoryByPath(newPath);
            if (targetDir && targetDir.type == 'directory') {
                this.currentPath = newPath;
            } else {
                this.appendOutput('<SPAN CLASS="error">cd: ' + path + ': No such file or directory</SPAN>\n');
            }
        }

        LinuxSim.prototype.makeDirectory = function(name) {
            if (!name) {
                this.appendOutput('<SPAN CLASS="error">mkdir: missing operand</SPAN>\n');
                return;
            }

            var currentDir = this.getCurrentDirectory();
            if (currentDir && currentDir.children) {
                if (currentDir.children[name]) {
                    this.appendOutput('<SPAN CLASS="error">mkdir: cannot create directory</SPAN>\n');
                } else {
                    currentDir.children[name] = { type: 'directory', children: new Object() };
                    this.appendOutput('<SPAN CLASS="success">Directory created</SPAN>\n');
                }
            }
        }

        LinuxSim.prototype.catFile = function(name) {
            if (!name) {
                this.appendOutput('<SPAN CLASS="error">cat: missing operand</SPAN>\n');
                return;
            }

            var currentDir = this.getCurrentDirectory();
            if (currentDir && currentDir.children && currentDir.children[name]) {
                var file = currentDir.children[name];
                if (file.type == 'file') {
                    this.appendOutput((file.content || '') + '\n');
                } else {
                    this.appendOutput('<SPAN CLASS="error">cat: ' + name + ': Is a directory</SPAN>\n');
                }
            } else {
                this.appendOutput('<SPAN CLASS="error">cat: ' + name + ': No such file</SPAN>\n');
            }
        }

        LinuxSim.prototype.showProcesses = function() {
            this.appendOutput('<TABLE>');
            this.appendOutput('<TR><TH>PID</TH><TH>USER</TH><TH>COMMAND</TH></TR>');
            
            for (var i = 0; i < this.processes.length; i++) {
                var proc = this.processes[i];
                this.appendOutput('<TR><TD>' + proc.pid + '</TD><TD>' + proc.user + '</TD><TD>' + proc.command + '</TD></TR>');
            }
            
            this.appendOutput('</TABLE>\n');
        }

        LinuxSim.prototype.clearScreen = function() {
            document.getElementById('output').innerHTML = '';
        }

        LinuxSim.prototype.getCurrentDirectory = function() {
            return this.getDirectoryByPath(this.currentPath);
        }

        LinuxSim.prototype.getDirectoryByPath = function(path) {
            var parts = path.split('/');
            var filteredParts = new Array();
            for (var i = 0; i < parts.length; i++) {
                if (parts[i] != '') filteredParts.push(parts[i]);
            }
            
            var current = this.fileSystem['/'];
            for (var i = 0; i < filteredParts.length; i++) {
                var part = filteredParts[i];
                if (current && current.children && current.children[part]) {
                    current = current.children[part];
                } else {
                    return null;
                }
            }
            return current;
        }

        LinuxSim.prototype.updatePrompt = function() {
            var prompt = document.getElementById('prompt');
            var displayPath = this.currentPath == '/home/user' ? '~' : this.currentPath;
            prompt.innerHTML = 'user@linux:' + displayPath + '$';
        }

        LinuxSim.prototype.appendOutput = function(text) {
            var output = document.getElementById('output');
            output.innerHTML += text;
            output.scrollTop = output.scrollHeight;
        }

        var simulator = new LinuxSim();
        //-->
    </SCRIPT>
</BODY>
</HTML>